<?php
class Back_UploadController extends Osml_Controller_Action_Upload
{

    public function init()
    {
        $options = array(
            'upload_dir' => realpath(APPLICATION_PATH), 
            'recup_url' => '/back/upload/recup/id/{ID}/file/', 
            'upload_url' => '/back/upload/upload/id/{ID}/file/', 
            'image_versions' => array(
                "thumbnail" => array(
                    'upload_dir' => realpath(APPLICATION_PATH), 
                    'upload_url' => '/back/upload/recupthumbnail/thumbnails/1/id/{ID}/file/', 
                    'max_width' => 80, 
                    'max_height' => 80
                )
            )
        );
        $this->setOptions($options);
    }

    public function recupthumbnailAction()
    {
        $this->desactivationRendu();
        $params = $this->getParams();
        $fichier = str_replace(".pdf", ".png", $params['file']);
        $thumbnails = $params["thumbnails"];
        $categorie = $params['categorie'];
        $id = $params['id'];
        
        switch ($categorie)
        {
            case "galerie":
                $album = new Back_Model_GalerieAlbums($id);
                $rep = strtolower(preg_replace("/\s/", "", $album->getTitre()));
            break;
            default:
                $rep = $id;
            break;
        }
        
        if ($thumbnails === false) {
            $this->get();
        } else {
            $optionsThb = $this->options['image_versions']['thumbnail'];
            if (is_file($optionsThb['upload_dir'] . DIRECTORY_SEPARATOR . "$categorie" . DIRECTORY_SEPARATOR . "$rep" . DIRECTORY_SEPARATOR . "thumbnail" . DIRECTORY_SEPARATOR . $fichier)) {
                header('Content-Type: image/png');
                echo file_get_contents($optionsThb['upload_dir'] . DIRECTORY_SEPARATOR . "$categorie" . DIRECTORY_SEPARATOR . "$rep" . DIRECTORY_SEPARATOR . "thumbnail" . DIRECTORY_SEPARATOR . $fichier);
            } else {
                die("image introuvable : " . $optionsThb['upload_dir'] . DIRECTORY_SEPARATOR . "$categorie" . DIRECTORY_SEPARATOR . "$rep" . DIRECTORY_SEPARATOR . "thumbnail" . DIRECTORY_SEPARATOR . $fichier);
            }
        }
    }

    /**
     *
     * @see Ddb_Controller_Action_Upload::getFilePath()
     */
    protected function getFilePath( $options, $filename )
    {
        $params = $this->getParams();
        switch ($params['categorie'])
        {
            case "galerie":
                $album = new Back_Model_GalerieAlbums($params['id']);
                $rep = strtolower(preg_replace("/\s/", "", $album->getTitre()));
            break;
        }
        return $options['upload_dir'] . DIRECTORY_SEPARATOR . $params["categorie"] . DIRECTORY_SEPARATOR . $rep . (! empty($filename) ? DIRECTORY_SEPARATOR . $filename : "");
    }

    public function getFileRacine( $categorie, $id )
    {
        $fileRacine = "";
        switch ($categorie)
        {
            case "galerie":
                $model = new Back_Model_GalerieAlbums($id);
                $fileRacine = preg_replace("/\s/", "", $model->getTitre());
            break;
        }
        return $fileRacine;
    }

    protected function getSpecificFilename( $file )
    {
        $id = $this->getRequest()->getParam("id", 0);
        $categorie = $this->getRequest()->getParam("categorie", "galerie");
        $filename = $file->name;
        $filename = Jnd_Outils::supprimeAccents($filename);
        switch ($categorie)
        {
            case "galerie":
            break;
        }
        return $filename;
    }

    public function getSpecificRep( $options = array() )
    {
        $categorie = $this->getRequest()->getParam("categorie", "galerie");
        $type = $this->getRequest()->getParam("type", "photos");
        $id = $this->getRequest()->getParam("id", 0);
        
        $album = new Back_Model_GalerieAlbums($id);
        $nomRep = strtolower(preg_replace("/\s/", "", $album->getTitre()));
        
        return DIRECTORY_SEPARATOR . "galerie" . DIRECTORY_SEPARATOR . $nomRep . DIRECTORY_SEPARATOR . (isset($options['version']) ? $options['version'] . "/" : "");
    }
}

